<?php 
 echo Yii::t('content','Imobiliário');
echo $this->renderPartial('//dashboard/mapinfo', array('model'=>$model)); 
 echo $this->renderPartial('//dashboard/mapform', array('model'=>$model)); 
echo $this->renderPartial('//dashboard/map', array('model' => $model));
 
 ?>
<div class="form well">

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'real-estate-form',
	'enableAjaxValidation'=>false,
)); ?>

	<?php echo $form->errorSummary($model); ?>
	 <div class="span3">
        <div class="well">
            <img class="right ic" src="/images/pt.gif"/>
            <div class="row">
                <?php echo $form->labelEx($model, 'title'); ?>
                <?php echo $form->textField($model, 'title', array('size' => 60, 'maxlength' => 255)); ?>
                <?php echo $form->error($model, 'title'); ?>
            </div>

            <div class="row">
                <?php echo $form->labelEx($model, 'description'); ?>
                <?php echo $form->textArea($model, 'description', array('size' => 60, 'maxlength' => 500)); ?>
                <?php echo $form->error($model, 'description'); ?>
            </div>
        </div>
        <div class="well">
            <img class="right ic" src="/images/en.gif"/>
            <div class="row">
                <?php echo $form->labelEx($model, 'title_en'); ?>
                <?php echo $form->textField($model, 'title_en', array('size' => 60, 'maxlength' => 255,'value'=>  $TitleEn)); ?>
                <?php echo $form->error($model, 'title_en'); ?>
            </div>

            <div class="row">
                <?php echo $form->labelEx($model, 'description_en'); ?>
                <?php echo $form->textArea($model, 'description_en', array('size' => 60, 'maxlength' => 500,'value'=>$DescEn)); ?>
                <?php echo $form->error($model, 'description_en'); ?>
            </div>
        </div>
        <div class="well">
            <img class="right ic" src="/images/de.gif"/>
            <div class="row">
                <?php echo $form->labelEx($model, 'title_de'); ?>
                <?php echo $form->textField($model, 'title_de', array('size' => 60, 'maxlength' => 255,'value'=>$TitleDe)); ?>
                <?php echo $form->error($model, 'title_de'); ?>
            </div>

            <div class="row">
                <?php echo $form->labelEx($model, 'description_de'); ?>
                <?php echo $form->textArea($model, 'description_de', array('size' => 60, 'maxlength' => 500,'value'=>$DescDe)); ?>
                <?php echo $form->error($model, 'description_de'); ?>
            </div>
        </div>
    </div>
<div class="span3">
	

	<div class="row">
		<?php echo $form->labelEx($model,'lat'); ?>
		<?php echo $form->textField($model,'lat'); ?>
		<?php echo $form->error($model,'lat'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'long'); ?>
		<?php echo $form->textField($model,'long'); ?>
		<?php echo $form->error($model,'long'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'square_meters'); ?>
		<?php echo $form->textField($model,'square_meters'); ?>
		<?php echo $form->error($model,'square_meters'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'country'); ?>
		<?php echo $form->textField($model,'country',array('size'=>60,'maxlength'=>255)); ?>
		<?php echo $form->error($model,'country'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'district'); ?>
		<?php echo $form->textField($model,'district',array('size'=>60,'maxlength'=>255)); ?>
		<?php echo $form->error($model,'district'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'local'); ?>
		<?php echo $form->textField($model,'local',array('size'=>60,'maxlength'=>255)); ?>
		<?php echo $form->error($model,'local'); ?>
	</div>
    </div>
    <div class="span2">
	<div class="row">
            <?php echo $form->labelEx($model, 'category_id'); ?>

            <?php echo $form->dropDownList($model, 'category_id', CHtml::listData(Category::model()->findAll(), 'id', 'name'), array('prompt' => Yii::t('content', 'Categoria'))); ?>

            <?php echo $form->error($model, 'category_id'); ?>
        </div>

	<div class="row">
		<?php echo $form->labelEx($model,'activ'); ?>
        <?php echo $form->dropDownList($model, 'activ', array('0' => Yii::t("content", 'Não'), '1' => Yii::t("content", 'Sim'))); ?>
		<?php echo $form->error($model,'activ'); ?>
	</div>

	<div class="row">
            <?php echo $form->labelEx($model, 'user_id'); ?>
            <?php echo $form->dropDownList($model, 'user_id', CHtml::listData(User::model()->findAll(), 'id', 'username'), array('prompt' => Yii::t('content', 'Utilizador'))); ?>

            <?php echo $form->error($model, 'user_id'); ?>
        </div>

	<div class="row">
		<?php echo $form->labelEx($model,'date_add'); ?>
		<?php echo $form->textField($model,'date_add',array('size'=>10,'maxlength'=>10,'class'=>'dtpk')); ?>
		<?php echo $form->error($model,'date_add'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'date_expire'); ?>
		<?php echo $form->textField($model,'date_expire',array('size'=>10,'maxlength'=>10,'class'=>'dtpk')); ?>
		<?php echo $form->error($model,'date_expire'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'amount'); ?>
		<?php echo $form->textField($model,'amount'); ?>
		<?php echo $form->error($model,'amount'); ?>
	</div>
</div>
    <div class="clearfix">
    </div>
	<div class="row buttons right">
        <?php echo CHtml::submitButton($model->isNewRecord ? Yii::t( 'content','Criar' ): Yii::t( 'content','Guardar' ),array('class'=>'btn btn-primary')); ?>
	</div>

<?php $this->endWidget(); ?>

</div><!-- form -->
<script type="text/javascript">




                        var geo = new GClientGeocoder();
                        var map = new GMap(document.getElementById("map"));
                        map.addControl(new GLargeMapControl3D());
                        map.addControl(new GMapTypeControl());
                        map.accuracy = 9;
                        map.centerAndZoom(new GPoint(<?php echo $model->long; ?>,<?php echo $model->lat; ?>), 14);
                        var point = new GPoint(<?php echo $model->long; ?>,<?php echo $model->lat; ?>);
                        map.addControl(new GOverviewMapControl(new GSize(100, 100)));
                        var marker = new GMarker(point, {draggable: true});
                        map.addOverlay(marker);
                        
                        GEvent.addListener(map, "click", clicked);
                       
                        // Recenter Map and add Coords by clicking the map
                        function showAddress(address) {
                            
                            if (geo) {
                                geo.getLatLng(
                                        address,
                                        function(point) {
                                            if (!point) {
                                                alert(address + " not found");
                                            } else {
                                                map.setCenter(point, 14);
                                                marker.setPoint(point)
                                                marker.openInfoWindowHtml(address);
                                            }
                                        }
                                );
                            }
                        }
                        function clicked(overlay, latlng) {
                            var point = marker.getPoint()

                            marker.setPoint(latlng);
                            map.panTo(latlng);
                            map.setZoom(15);
                            map.setCenter(latlng);
                            map.closeInfoWindow();
                            if (point) {
                                geo.getLocations(latlng, function(addresses) {
                                    if (addresses.Status.code != 200) {
                                        alert("reverse geocoder failed to find an address for " + latlng.toUrlValue());
                                    }
                                    else {
                                        address = addresses.Placemark[0];
                                        var myHtml = address.address;
                                        var where_is_mytool = myHtml;
                                        if (confirm('Aceita este endereço?\n\n\n' + myHtml)) {
                                          
                                            document.getElementById("RealEstate_lat").value = point.y
                                            document.getElementById("RealEstate_long").value = point.x
                                            function crawl(r, a) {
                                                if (typeof r[a] == "string" || typeof r[a] == "number") {
                                                    
                                                    if (a == "LocalityName") {
                                                        //  alert( "CONCELHO:" + r[a] + " ?");
                                                        document.getElementById("RealEstate_local").value = r[a];
                                                    }
                                                    if (a == "AdministrativeAreaName") {
                                                        //  alert("DISTRITO:" + r[a] + " ?");
                                                        document.getElementById("RealEstate_district").value = r[a];
                                                    }
                                                    if (a == "CountryName") {
                                                        // alert("País:" + r[a] + " ?");
                                                        document.getElementById("RealEstate_country").value = r[a];
                                                    }
                                                    
                                                } else {
                                                    // It's still a complex Object, so crawl inside
                                                    for (b in r[a]) {
                                                        crawl(r[a], b);
                                                    }
                                                }
                                            }
                                        }
                                        // Initiate the crawling
                                        for (a in addresses.Placemark[0]) {
                                            crawl(addresses.Placemark[0], a);
                                        }
                                    }
                                });
                            }
                        }
                        

</script>